package com.demo.house.service.impl;


import com.demo.house.common.FileUtil;
import com.demo.house.dao.HouseSourseDao;
import com.demo.house.dao.RepairDao;
import com.demo.house.entity.HouseSourse;
import com.demo.house.entity.HouseUrl;
import com.demo.house.entity.Repair;
import com.demo.house.service.RepairService;
import com.demo.house.vo.request.RequestRepairVo;
import com.demo.house.vo.response.ResponseRepairSelectLandlordVo;
import com.demo.house.vo.response.ResponseSelectMySelf;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
public class RepairServiceImpl implements RepairService {

    @Autowired
    private RepairDao repairDao;
    private final String uploadPath="D:\\img\\";
    @Autowired
    private HouseSourseDao houseSourseDao;

    /**
     * 新增报修申请
     * @param repair
     * @return
     */


    @Override
    public int insertSelective(Repair repair) {
        int i = repairDao.insertSelective(repair);
        System.out.println(i);
        System.out.println(repair);
        return repair.getId();
    }
    @Override
    public String upp(Integer id,Integer houseSourseId,Integer userId,Integer urlId, MultipartFile file) {
        //相对路径
        String relativePath  = "files" + File.separator + "img"  + File.separator;
        //绝对路径
        String absolutePath = uploadPath + relativePath;
        //执行文件上传
        System.out.println(id);
        File upload = FileUtil.upload(file, absolutePath);
        if (!ObjectUtils.isEmpty(upload)) {

           Repair repair = repairDao.selectByPrimaryKey(id);

            repair.setId(id);
            repair.setHouseSourseId(houseSourseId);
            repair.setUserId(userId);

            if (urlId==1){
            repair.setUrl1(relativePath + upload.getName());
            }
            if (urlId==2){
                repair.setUrl2(relativePath + upload.getName());
            }
            if (urlId==3){
                repair.setUrl3(relativePath + upload.getName());
            }
            repairDao.updateByPrimaryKey(repair);
            return repair.getUrl1()+ repair.getUrl2()+repair.getUrl3();
        }
        else {
            return null;
        }
    }

    /**
     * 租客查询报修
     * @param id
     * @return
     */
    @Override
    public List<Repair> selectById(Integer id) {
        return repairDao.selectById(id);
    }

    /**
     * 租客删除报修
     * @param id
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Integer id) {
       return repairDao.deleteByPrimaryKey(id);
    }


    /**
     * 房东查询报修
     * @param id
     * @return
     */
    @Override
    public List<Repair> selectRepair(Integer id) {
        return repairDao.selectRepair(id);
    }


    /**
     * 审核报修
     * @param id
     * @return
     */
    @Override
    public int Review(Integer id) {
        Repair repair = repairDao.selectByPrimaryKey(id);
        repair.setState(1);
        return repairDao.updateByPrimaryKey(repair);
    }
    /*图片显示*/
    @SneakyThrows
    @Override
    public BufferedImage getRepairUrl(Integer id,Integer urlId) {
        Repair byid = repairDao.getRepairUrl(id);

        String path = "";
        System.out.println("Repair."+byid);

        if (urlId==1){
             path = uploadPath+byid.getUrl1();
        }
        if (urlId==2){
             path = uploadPath+byid.getUrl2();
        }
        if (urlId==3){
             path = uploadPath+byid.getUrl3();
        }

        System.out.println(path);
        File file = new File(path);
        BufferedImage read = ImageIO.read(file);
        return read;



    }


}
